import React, {Component} from "react";
import {nanoid} from "nanoid";
import PropTypes from "prop-types";
import './index.css'

export class UserHeader extends Component {
    
    static propTypes = {
        addTodo:PropTypes.func.isRequired
    }
    
    render() {
        return (
            <div className="todo-header">
                <input type="text" onKeyUp={this.handleKeyUp} placeholder="请输入你的任务名称，按回车键确认"/>
            </div>
        )
    }
    
    handleKeyUp = (e) => {
        const {target, keyCode} = e;
        if (keyCode !== 13) return
        if (target.value.trim() === '') {
           return window.alert("名称不可以为空");
        }
        const todoObj = {
            title: target.value,
            id: nanoid(),
            done: false
        }
        this.props.addTodo(todoObj)
        target.value = ''
        
    }
}